package net.uman.framework.modules.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.uman.framework.io.FileManagerFactory;
import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.web.struts2.Struts2Utils;

public class ReportGenerator {
	public String generateReport(String name, List<PropertyFilter> props) {
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			if (props != null) {
				for (PropertyFilter filter : props) {
					params.put(filter.getPropertyName(), filter.getPropertyValue());
				}
			}
			
			DataSource dataSource = (DataSource) ApplicationContextProvider.getBean("dataSource");
			
			InputStream is = FileManagerFactory.getFileManager().getFile(
					"reports/" + name + ".jrxml").getInputStream();
			JasperReport report = JasperCompileManager.compileReport(is);
			is.close();
			JasperPrint print = JasperFillManager.fillReport(report, params,
					dataSource.getConnection());

			// transaction.rollback();
			// session.close();

			JRHtmlExporter exporter = new JRHtmlExporter();

//			OutputStream os = FileManagerFactory.getFileManager().createFile(
//					"reports/result/" + name + ".html").getOutputStream();

			
			StringWriter writer = new StringWriter();
			
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
			exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, writer);
			exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML,
					"");
			exporter.setParameter(
					JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					Boolean.TRUE);
			// exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
			// Boolean.FALSE);
			exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
					Struts2Utils.getRequest().getContextPath()+"/b2b/report!readFile.action?name=reports/result/" + name + ".html_files/");
			exporter.setParameter(
					JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR,
					Boolean.FALSE);

			Map images = new HashMap();

			exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, images);

			exporter.exportReport();

			writer.close();

			for (Iterator<String> it = images.keySet().iterator(); it.hasNext();) {
				String imageName = it.next();
				byte[] content = (byte[]) images.get(imageName);

				FileManagerFactory.getFileManager().createFile(
						"reports/result/" + name + ".html_files/" + imageName)
						.saveContent(content);
			}
			
			return writer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JRException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

//		return "reportService/reports/result/" + name + ".html";
		return "";
	}
}
